# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

import os
class NovelDownloadMoreSourcePipeline:
    def __init__(self):
        self.items = []

    def process_item(self, item, spider):
        self.items.append(item)
        return item

    def close_spider(self, spider):
        if self.items:
            with open(r"{}\novels\{}.txt".format(os.getcwd(), spider.novel_name), "w", encoding="utf-8") as f:
                self.items.sort(key=lambda i: i['num'])
                for item in self.items:
                    f.write('\n' + item['chapter_name'])
                    f.write('\n' + item['chapter_content'] + '\n')
        # logging.warning('准备结束了{}'.format(os.getpid()))
        # a= os.system('taskkill /f /pid {}'.format(os.getpid()))
        # logging.warning(a)
